<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="./lib/vue-2.4.0.js"></script>
    <script src="./lib/vue-router-3.0.1.js"></script>
    <link rel="stylesheet" href="./lib/bootstrap-3.3.7.css">
    <style>
        .router-link-active,
        .myactive {
            font-size: 30px;
            color: red;
        }
        
        .ff-enter,
        .ff.leave-to {
            opacity: 0;
            transform: translateX(140px);
        }
        
        .ff-enter-active,
        .ff-leave-active {
            transition: all 0.5s ease;
        }
    </style>
</head>

<body>
    <template id="login">
        
        <button type="button" class="btn btn-success">登录</button>
        
    </template>

    <template id="reg">
        
        <button type="button" class="btn btn-primary">注册</button>
        
    </template>
    <div id="app">
        <!-- <a href="#/login">登录</a>
        <a href="#/reg">注册</a> -->

        <!-- router-link 默认渲染为一个a 标签  tag="span" 解析未span
        -->
        <router-link to="/login">登录</router-link>
        <router-link to="/reg">注册</router-link>
        <p></p>
        <transition name="ff" mode="out-in">
            <router-view></router-view>
        </transition>


        <input type="text" v-model="key">
        <div>{{key}}</div>
    </div>

    <script>
        var login = {
            template: "#login"
        }
        var reg = {
            template: "#reg"
        }


        // 创建路由的时候，传递一个配置对象
        var rout = new VueRouter({
            // route:表示路由匹配规则
            routes: [ //路由匹配规则 path:监听的路由， component:表示匹配那个组件进行显示
                {
                    path: '/',
                    redirect: '/login' //redirect重定向到login
                }, {
                    path: "/login",
                    component: login,
                }, {
                    path: "/reg",
                    component: reg,
                }
            ],
            //配置激活类
            linkActiveClass: "myactive"
        })
        var vm = new Vue({
            el: '#app',
            data: {
                key: ""
            },
            methods: {},
            watch: {
                //监听值的变化
                key: function(newVal, oldVal) {
                    console.log(newVal)
                    console.log(oldVal)
                },
                "$route.path": function(newVal, oldVal) {

                    console.log("监听到路由发的变化 old：", oldVal)
                    console.log("监听到路由发的变化 new：", newVal)
                }
            },
            router: rout, //将路由规则对象注册到vm实例上
        });
    </script>
</body>

</html>